/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//Olá profº,na hora de corrigir ao abrir o trabalho talvez o código do projeto fique com com erros
//devido a versão do toolkit JavaME,para rodar ir em Propriedades,na janela selecionar
// plataforma e setar todas opções!!!
package ohm;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author William
 */
public class HelloMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command entradaCommand;
    private Command itemCommand1;
    private Command itemCommand2;
    private Command backCommand;
    private Command resultCommand;
    private Command backCommand1;
    private Form ohmcalc;
    private Spacer spacer;
    private TextField voltsTextField;
    private TextField ampereTextField;
    private Form resultado;
    private StringItem resultadoStringItem;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public static String formatação(double valor) {
       double numero = valor;
        int numero1 = (int) numero;
        int numero2 = (int) ((numero - numero1) * 100);
        String saída = numero1 + "." + numero2;

       return saída;
   }

   public HelloMIDlet() {
       
   }
   

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
       
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getOhmcalc());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display;
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == ohmcalc) {//GEN-BEGIN:|7-commandAction|1|45-preAction
            if (command == backCommand1) {//GEN-END:|7-commandAction|1|45-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|45-postAction
                // write post-action user code here
            } else if (command == entradaCommand) {//GEN-LINE:|7-commandAction|3|29-preAction

                try {
                    double v = Double.parseDouble(voltsTextField.getString());
                    //declaração de variável para contar os volts
                    double amp = Double.parseDouble(ampereTextField.getString());
                    //para saber o valor da amperagem (corrente)
                    double res = v / amp;
                    // para poder saber o resultado final

                    switchDisplayable(null, getResultado());//GEN-LINE:|7-commandAction|4|29-postAction

                    //resultadoStringItem.setText(Double.toString(res));
                    resultadoStringItem.setText(formatação(res));
                }
                catch(Exception ex){
                    Alert msg = new Alert("Erro!!!");
                    msg.setString("Os valores digitados estão errados");
                    msg.setTimeout(4000);
                    getDisplay().setCurrent(msg);
                }
            }//GEN-BEGIN:|7-commandAction|5|39-preAction
        } else if (displayable == resultado) {
            if (command == backCommand) {//GEN-END:|7-commandAction|5|39-preAction
                // write pre-action user code here
                switchDisplayable(null, getOhmcalc());//GEN-LINE:|7-commandAction|6|39-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
//</editor-fold>//GEN-END:|7-commandAction|8|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|18-getter|2|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: ohmcalc ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of ohmcalc component.
     *
     * @return the initialized component instance
     */
    public Form getOhmcalc() {
        if (ohmcalc == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            ohmcalc = new Form("OhmCalc", new Item[]{getSpacer(), getVoltsTextField(), getAmpereTextField()});//GEN-BEGIN:|22-getter|1|22-postInit
            ohmcalc.addCommand(getEntradaCommand());
            ohmcalc.addCommand(getBackCommand1());
            ohmcalc.setCommandListener(this);//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return ohmcalc;
    }
//</editor-fold>//GEN-END:|22-getter|2|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: entradaCommand ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initialized instance of entradaCommand component.
     *
     * @return the initialized component instance
     */
    public Command getEntradaCommand() {
        if (entradaCommand == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            entradaCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return entradaCommand;
    }
//</editor-fold>//GEN-END:|28-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand1 ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initialized instance of itemCommand1 component.
     *
     * @return the initialized component instance
     */
    public Command getItemCommand1() {
        if (itemCommand1 == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            itemCommand1 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return itemCommand1;
    }
//</editor-fold>//GEN-END:|30-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand2 ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initialized instance of itemCommand2 component.
     *
     * @return the initialized component instance
     */
    public Command getItemCommand2() {
        if (itemCommand2 == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            itemCommand2 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return itemCommand2;
    }
//</editor-fold>//GEN-END:|32-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initialized instance of spacer component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 1);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return spacer;
    }
//</editor-fold>//GEN-END:|27-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultCommand ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initialized instance of resultCommand component.
     *
     * @return the initialized component instance
     */
    public Command getResultCommand() {
        if (resultCommand == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            resultCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return resultCommand;
    }
//</editor-fold>//GEN-END:|36-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|38-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultado ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initialized instance of resultado component.
     *
     * @return the initialized component instance
     */
    public Form getResultado() {
        if (resultado == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            resultado = new Form("Resultado", new Item[]{getResultadoStringItem()});//GEN-BEGIN:|34-getter|1|34-postInit
            resultado.addCommand(getBackCommand());
            resultado.setCommandListener(this);//GEN-END:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return resultado;
    }
//</editor-fold>//GEN-END:|34-getter|2|
    //</editor-fold>
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultadoStringItem ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initialized instance of resultadoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getResultadoStringItem() {
        if (resultadoStringItem == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            resultadoStringItem = new StringItem("Resist\u00EAncia em Ohms", null);//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return resultadoStringItem;
    }
//</editor-fold>//GEN-END:|41-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: voltsTextField ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initialized instance of voltsTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getVoltsTextField() {
        if (voltsTextField == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            voltsTextField = new TextField("Digite o Valor da Tens\u00E3o (volts)", null, 32, TextField.DECIMAL);//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here
        }//GEN-BEGIN:|42-getter|2|
        return voltsTextField;
    }
//</editor-fold>//GEN-END:|42-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: ampereTextField ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initialized instance of ampereTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getAmpereTextField() {
        if (ampereTextField == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            ampereTextField = new TextField("Digite o Valor da corrente (Ampere)", null, 32, TextField.DECIMAL);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return ampereTextField;
    }
//</editor-fold>//GEN-END:|43-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initialized instance of backCommand1 component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            backCommand1 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|44-getter|1|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|2|
        return backCommand1;
    }
//</editor-fold>//GEN-END:|44-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
